package com.touch.workshop.ui.scripteditor;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;

public class ScriptEditorMenuEvents implements ActionListener
{

	public void actionPerformed(ActionEvent e) 
	{	
		final String command = e.getActionCommand();
	
		if(command.equals("Script"))
		{
			String name = JOptionPane.showInputDialog(null, "Script Name");
			if(name == null)
				return;
			if(name.length() == 0)
				return;
			if(!name.toLowerCase().endsWith(".js"))
				name = name + ".js";
			ScriptEditorControls.SCRIPTS.put(name, null);
			DefaultMutableTreeNode newScript = new DefaultMutableTreeNode(name);
			
			ScriptEditorControls.SCRIPT_EDITOR.root.add(newScript);
		}
		else if(command.equals("Function"))
		{
			String name = JOptionPane.showInputDialog(null, "Function Name");
			if(name == null)
				return;
			if(name.length() == 0)
				return;
			DefaultMutableTreeNode path = (DefaultMutableTreeNode)
				ScriptEditorControls.SCRIPT_EDITOR.treeBlocks.getSelectionPath().getLastPathComponent();
			if(path.getParent().toString().equals("Scripts"))
				path.add(new DefaultMutableTreeNode(name));
			else
				return;
			String script = ScriptEditorControls.SCRIPTS.get(path.toString());
			if(script == null)
				script = "";
			script = script + "function " + name + "()\n{\n\n}";	
			ScriptEditorControls.SCRIPTS.put(path.toString(), script);
			ScriptEditorControls.SCRIPT_EDITOR.text.setText(script);			
		}
		else
			System.out.println("//TODO: Implement action: " + command);
	}

}
